Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
 SqlConnection con = new SqlConnection("data source=SAMIRAN-PC; Initial catalog = DB2; Integral security= true");
    SqlDataAdapter dat = new SqlDataAdapter();
    dat.InsertCommand = new SqlCommand(" INSERT INTO TAB2 VALUES(@NAME,@AGE)", con);
    dat.InsertCommand.Parameters.Add("@NAME",SqlDbType.VarChar).Value = textBox1.Text;
    dat.InsertCommand.Parameters.Add("@AGE", SqlDbType.VarChar).Value = textBox2.Text;
    con.Open();
    dat.InsertCommand.ExecuteNonQuery();
    con.Close();
}


at the first line it says like such error.Nd I cannot find any thing wrong. Still it is happening.
If any one can do, will be most welcome !!


Thank You !!

What I have tried:

There may be connection problem.I am doing with such way. But I don't know which is the best and correct way to connect. Help !
Posted
Updated 5-Apr-16 17:31pm
v2

Your connection string is not correct. Update it to:

Data Source=SAMIRAN-PC; Initial Catalog = DB2; Integrated Security= SSPI


This connection string specifies the server (data source), database (intial catalog) and mentions that windows authentication should be used (integrated security). In your code, last parameter is not correct.
 
Share this answer
 
v2
Comments
KukiSamiran 5-Apr-16 23:36pm    
after doing that there is another problem
in
con.Open();
dan!sh 5-Apr-16 23:49pm    
You will need to provide details. What is the exception you are getting? Paste the message here. I suspect windows authentication is not enabled or you do not have enough permissions on the database.
KukiSamiran 6-Apr-16 0:56am    
this problem is solved. Thanks :)
another problem is that
"SqlException was unhandled" -> in con.Open() command highlighting.
KukiSamiran 6-Apr-16 5:30am    
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
I would try to replace:
C#
dat.InsertCommand.Parameters.Add("@AGE", SqlDbType.VarChar).Value = textBox2;

with :
C#
dat.InsertCommand.Parameters.Add("@AGE", SqlDbType.VarChar).Value = textBox2.text;

You may also need to convert textBox2.text to numeric.
 
Share this answer
 
Comments
KukiSamiran 5-Apr-16 23:36pm    
sorry ! this was my mistake. I have not paste it here correctly .

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