Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
menu
Starting Member
4 Posts |
Posted - 2005-09-09 : 23:45:54
|
Hi all,I am unable to insert a record into sql server with asp.net/c#. could you please let me know what I am doing wrong. Here is the code: string username = mlblUserName.Text.ToString(); string stdpolicy = four.Checked.ToString(); string sql = "insert into table (UserName, StdPolicy) values ('"+username+"','"+stdpolicy+"')"; SqlConnection conn = new SqlConnection( connString ); SqlCommand command = new SqlCommand(sql,conn); conn.Open(); command.ExecuteNonQuery(); conn.Close(); If i insert giving random values like: string sql = "insert into table (UserName, StdPolicy) values ('menu','0')"; it works but it does'nt work for the above statementI do not want to write any stored proc as this is all iam doing right now with the table.Any help is greatly appreciated.Thanks |
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2005-09-10 : 07:10:54
|
do you get any error??copy/paste the already constructed sql string in QA and run it there.Does that insert the record?Go with the flow & have fun! Else fight the flow |
 |
|
menu
Starting Member
4 Posts |
Posted - 2005-09-10 : 09:45:07
|
When i copy and paste, it inserts a record but inserts only the names i have provided not the value of the names.insert into IMAuthorization (UserName, ScreenName, ) values ('"+username+"','"+screename+"'). It stores: +username+ +screename+ and not the value of them. I have given string username = TextBox1.Text.toString();string screenname = TextBox2.Text.tostring();Thanks for your reply |
 |
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2005-09-10 : 11:05:35
|
that's not what i meant.when debuging the code you can use watches to see what values the variables have.add a watch to sql variable and see what value it has after this line:string sql = "insert into table (UserName, StdPolicy) values ('"+username+"','"+stdpolicy+"')";the put that value in the qa.Go with the flow & have fun! Else fight the flow |
 |
|
menu
Starting Member
4 Posts |
Posted - 2005-09-10 : 13:39:46
|
It works but with a problem. I had written all the above code inside (Page.Isvalid)in the submit button function and i took it out and now it works, do u know the reason why?. and other thing, how do i pass the value of the radiobutton. like i have: insert into table (UserName,StdPolicy,ScreenName) values ('"+mlblUserName.Text+"','"+four.checked+"',"+TextBox1.Text+"')"; It shows me an error in ASP.net page : The name 'com' is not permitted in this context. Only constants, expressions, or variables allowed here. Column names are not permitted. Unclosed quotation mark before the character string ')'. and points to command.ExecuteNonQuery(). If i remove, radiobutton column, it works fine. Thanks |
 |
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2005-09-10 : 13:44:28
|
well i guess you should post your code...i personaly never use radio buttons because i hate them.instead i use a combobox beause it has the same functionality and it's simple to work with.Go with the flow & have fun! Else fight the flow |
 |
|
menu
Starting Member
4 Posts |
Posted - 2005-09-10 : 17:19:01
|
it works now, i had missed a single quotation. I am soo sorry about that, but i still have problem if i write protected void submit(object sender, System.EventArgs e){if (Page.Isvalid){ string username = mlblUserName.Text.ToString();string stdpolicy = four.Checked.ToString();string sql = "insert into table (UserName, StdPolicy) values ('"+username+"','"+stdpolicy+"')";SqlConnection conn = new SqlConnection( connString );SqlCommand command = new SqlCommand(sql,conn);conn.Open();command.ExecuteNonQuery();conn.Close(); }it does'nt write anything to sql server if i give (page.isvalid),but i need that because i need to write to database only if the user has entered correct values. Iam new to asp.nt/sql server. thanks |
 |
|
|
|
|
|
|