Showing posts with label easiest. Show all posts
Showing posts with label easiest. Show all posts

Friday, March 30, 2012

Inserting a checkbox value into bit field sql server 2000

Edited by SomeNewKid. Please post code between<code> and</code> tags.



This is probaly the easiest question you've ever read but here goes.

I have a simple checkbox value that i want to insert into the database but whatever i do it does not seem to let me.

Here is my code:

Sub AddSection_Click(Sender As Object, e As EventArgs)
Dim myCommand As SqlCommand
Dim insertCmd As String
' Build a SQL INSERT statement string for all the input-form
' field values.
insertCmd = "insert into Customers values (@.SectionName, @.SectionLink, @.Title, @.NewWindow, @.LatestNews, @.Partners, @.Support);"
' Initialize the SqlCommand with the new SQL string.
myCommand = New SqlCommand(insertCmd, myConnection)
' Create new parameters for the SqlCommand object and
' initialize them to the input-form field values.
myCommand.Parameters.Add(New SqlParameter("@.SectionName", SqlDbType.nVarChar, 50))
myCommand.Parameters("@.SectionName").Value = Section_name.Value

myCommand.Parameters.Add(New SqlParameter("@.SectionLink", SqlDbType.nVarChar, 80))
myCommand.Parameters("@.SectionLink").Value = Section_link.Value

myCommand.Parameters.Add(New SqlParameter("@.Title", SqlDbType.nVarChar, 50))
myCommand.Parameters("@.Title").Value = Section_title.Value

If New_window.Checked = false Then
myCommand.Parameters.Add(New SqlParameter("@.NewWindow", SqlDbType.bit, 1))
myCommand.Parameters("@.NewWindow").Value = 0
else
myCommand.Parameters.Add(New SqlParameter("@.NewWindow", SqlDbType.bit, 1))
myCommand.Parameters("@.NewWindow").Value = 1
End If

If Latest_news.Checked = false Then
myCommand.Parameters.Add(New SqlParameter("@.LatestNews", SqlDbType.bit, 1))
myCommand.Parameters("@.LatestNews").Value = 0
else
myCommand.Parameters.Add(New SqlParameter("@.LatestNews", SqlDbType.bit, 1))
myCommand.Parameters("@.LatestNews").Value = 1
End If

If Partners.Checked = false Then
myCommand.Parameters.Add(New SqlParameter("@.Partners", SqlDbType.bit, 1))
myCommand.Parameters("@.Partners").Value = 0
else
myCommand.Parameters.Add(New SqlParameter("@.Partners", SqlDbType.bit, 1))
myCommand.Parameters("@.Partners").Value = 1
End If

If Support.Checked = false Then
myCommand.Parameters.Add(New SqlParameter("@.Support", SqlDbType.bit, 1))
myCommand.Parameters("@.Support").Value = 0
else
myCommand.Parameters.Add(New SqlParameter("@.Support", SqlDbType.bit, 1))
myCommand.Parameters("@.Support").Value = 1
End If

myCommand.Connection.Open()
' Test whether the new row can be added and display the
' appropriate message box to the user.
Try
myCommand.ExecuteNonQuery()
Message.InnerHtml = "Record Added<br>" & insertCmd
Catch ex As SqlException
If ex.Number = 2627 Then
Message.InnerHtml = "ERROR: A record already exists with " _
& "the same primary key"
Else
Message.InnerHtml = "ERROR: Could not add record, please " _
& "ensure the fields are correctly filled out"
Message.Style("color") = "red"
End If
End Try

myCommand.Connection.Close()
BindGrid()
End Sub

Any response would be appreciatedYou're right; it's easier than you think. Try this:

myCommand.Parameters.Add(New SqlParameter("@.Partners", SqlDbType.bit, 1))
myCommand.Parameters("@.Partners").Value = Partners.Checked

|||thanks a lot and sorry for the late reply that worked

Friday, March 9, 2012

insert row question

Hello all, my main question is i guess, is this the easiest way?
INSERT INTO table1 (c1, c2, c3...)
SELECT number1 AS c1, c2, c3...
FROM table1
WHERE c1 = number2
Just so there is no confusion: I am only working with one table here. I
basically want to copy a row where c1 = some_number, and paste on a new row
where c1 = different_number: Here is a crude example:
some_number, c2, c3, c4...
different_number, c2, c3, c4...
The reason I ask is beacuse the columns can sometimes extend way out there,
it is not very convenient to get the names and list them all out. I have
saved code to run a lot of the queries I need just as anyone would do, but
they are not always in hand.
I was looking for something real simple, but that may be asking to much.
Would a cursor, or using a temp table be better? Or just stick to way above?
I really hope this is all not to confusing, and thank you all very much.
Josh.Just so I understand, you have a table with some number of rows in it,
like so:
5, 10, 15
6, 11, 26
And you want to add an additional row for every row in the table where
the first column = some value (say 5 in this case), but you want to
change that value to be something else (7), eg.
7, 10, 15
So your results would be
5,10,15
6, 11, 26
7, 10, 15
Correct?
If so, your method is fine.
Stu|||Stu, that is close to what is happening. The colums are a variety of data
types, not all numbers. Also, I do not always want to add them in sequential
order. The first colum is an ID number that associates all rows with that
number together, I will use all numbers as an example since it is easiest:
7, 4, 2, 6, 5
7, 9, 6, 2, 6
3, 2, 5, 8, 9
6, 8, 3, 1, 7
4, 0, 5, 7, 3
4, 0, 5, 8, 2
4, 9, 4, 2, 3
Now lets say I want to take that very first row of data and make a copy to
add to the group starting with 4.
So after running the query end up with something like this:
7, 4, 2, 6, 5
7, 9, 6, 2, 6
3, 2, 5, 8, 9
6, 8, 3, 1, 7
4, 0, 5, 7, 3
4, 0, 5, 8, 2
4, 9, 4, 2, 3
4, 4, 2, 6, 5
I know that is not really far from what you had wrote but I just thought I
would clarify. Do you still think the best way is with what I am doing now?
Thank you, Josh
Stu wrote:
>Just so I understand, you have a table with some number of rows in it,
>like so:
>5, 10, 15
>6, 11, 26
>And you want to add an additional row for every row in the table where
>the first column = some value (say 5 in this case), but you want to
>change that value to be something else (7), eg.
>7, 10, 15
>So your results would be
>5,10,15
>6, 11, 26
>7, 10, 15
>Correct?
>If so, your method is fine.
>Stu|||Sorry, my example was a poor fit; yes, you should be OK.
Stu