Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
{
            string Username = UserNTB.Text;
            string password = UserPass.Text;
            string Address = UserAdd.Text;

            SqlConnection con = new SqlConnection();
            con.ConnectionString = "Data Source=PROMOD-PC;Initial Catalog=UserDB;Integrated Security=True";
            
            String QueryStr = "Insert into UserInfo(Username, Password, Address) Values (@UN, @UP, @UA)";

            SqlCommand com = new SqlCommand(QueryStr, con);

            com.Parameters.Add(new SqlParameter("@UN", UserNTB.Text));
            com.Parameters.Add(new SqlParameter("@UP", UserPass.Text));
            com.Parameters.Add(new SqlParameter("@UA", UserAdd.Text));


            con.Open();
            com.ExecuteNonQuery();


        }


when i input data to my interface and when i hit okay button it will point out con.open and gives exception unhndled error?
Posted
Comments
PIEBALDconsult 25-Feb-14 23:04pm    
Well? What's the error?
♥…ЯҠ…♥ 25-Feb-14 23:10pm    
I would suggest you to put it in Try catch block and see the error it throws and come back again with error description
King Fisher 25-Feb-14 23:44pm    
use try catch block.

Check the query itself.
Try running it in SQL Server Management Studio.

Ensure all database values are of the correct type.
 
Share this answer
 
C#
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=PROMOD-PC;Initial Catalog=UserDB;Integrated Security=True";

try
{
    string Username = UserNTB.Text;
    string password = UserPass.Text;
    string Address = UserAdd.Text;
    
    String QueryStr = "Insert into UserInfo(Username, Password, Address) Values (@UN, @UP, @UA)";
    
    con.Open();
    SqlCommand com = new SqlCommand(QueryStr, con);  // here it requires Open Connection to the database
    
    com.Parameters.Add(new SqlParameter("@UN", UserNTB.Text));
    com.Parameters.Add(new SqlParameter("@UP", UserPass.Text));
    com.Parameters.Add(new SqlParameter("@UA", UserAdd.Text));
    
    com.ExecuteNonQuery();

}
finally
{
    con.Close();
}


-KR
 
Share this answer
 
Comments
PIEBALDconsult 25-Feb-14 23:15pm    
No it doesn't.
Krunal Rohit 25-Feb-14 23:19pm    
Yeah of course, SqlCommand requires an Open conncetion.
Put your code try-catch-finally block and catch the exception and see what happens.

-KR
PIEBALDconsult 25-Feb-14 23:20pm    
I never Open a Connection until after I assign the CommandText and set the Parameters.
Krunal Rohit 25-Feb-14 23:22pm    
see my answer, before assigning it to command I've opened connection and in finally it is Closed.

-KR
PIEBALDconsult 25-Feb-14 23:28pm    
Yes, you have, but even the MSDN example shows Opening the Connection after instantiating the Command.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900