.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> ASP.Net >> Post New QuestionBookmark and Share Subscribe to Forum

why cmd.Parameters.Add("@User", SqlDbType.VarChar,20).Value = txtUser.Text; not working

Posted By: shweta     Posted Date: August 23, 2010    Points:2   Category :ASP.Net
Hi All,
I am unable to work with stored procedure with input as a string value. My code is
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Constr"].ToString());
SqlCommand cmd = new SqlCommand("GetUserInfo", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@User", SqlDbType.VarChar,20).Value = txtUser.Text;
cmd.Parameters.Add("@encrypted", SqlDbType.VarChar,50).Value = encodedData;
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
string popupScript = "<script language='javascript'>" + "alert('Username or Password already exist');" + "</script>";
ClientScript.RegisterStartupScript(this.GetType(), "PopupScript", popupScript);
return;
}

control is unable to read data from database and alert is not showing up.I am not getting what is the problem in this code.
Kindly reply soon.
Thanks,
Shweta

Responses
Author: Nikhil Kumar             
Posted Date: August 23, 2010     Points: 5   

I think you should follow this pattern to be right...

private bool fnlog(string user, string pass)
{
SqlConnection con = new SqlConnection(connection);
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "prc_login";
cmd.CommandType=CommandType.StoredProcedure;
cmd.Connection=con;
cmd.Parameters.Add(new SqlParameter("@user", textBox1.Text));
cmd.Parameters.Add(new SqlParameter("@pass", textBox2.Text));
cmd.Parameters.Add(new SqlParameter("@date", DateTime.Now));
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
return true;
}
else
{
return false;
}
dr.Close();
}

thank you :)

Be helpful and enjoy life because it comes only onces. Give all the happiness to yours and remember it doesn't comes by money ...
Author: Rajshree             
Posted Date: September 16, 2010     Points: 5   

Hi
Use the below string for SqlConnection

SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Constr"].ConnectionString);


Use ConnectionString Property rather than toString()

Please try this.





Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend