data binding in text box using stored procedure [Resolved]

Posted by Jiturcm under C# on 4/15/2011 | Points: 10 | Views : 8033 | Status : [Member] | Replies : 2
there is 3 text box named as id,name,age if i entered id then name, age is bind respective to textbox id but when i did n't enterd the value in textbox id then it creates error how can i avoid the error
here is the code
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=ORCHE5;Initial Catalog=test;Integrated Security=True;Password=sqlserver2005");
con.Open();
SqlDataReader dr;
SqlCommand cmd = new SqlCommand("prc_search", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@TextBox1", System.Data.SqlDbType.Int));
cmd.Parameters["@textBox1"].Value = TextBox1.Text;
dr = cmd.ExecuteReader();
while (dr.Read())
{

TextBox2.Text = dr[0].ToString();
TextBox3.Text = dr[1].ToString();
}

}

jitendra kumar mahapatro


Responses

Posted by: Jiturcm on: 4/18/2011 [Member] Starter | Points: 25

Up
0
Down

Resolved
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=ORCHE5;Initial Catalog=test;Integrated Security=True;Password=sqlserver2005");
SqlDataAdapter adp = new SqlDataAdapter("prc_search", con);
adp.SelectCommand.CommandType = CommandType.StoredProcedure;
if (TextBox1.Text == "")
{
adp.SelectCommand.Parameters.Add(new SqlParameter("@TextBox1", System.Data.SqlDbType.Int));
adp.SelectCommand.Parameters["@textBox1"].Value = 0;
DataSet ds = new DataSet();
adp.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
GridView1.Visible = true;
TextBox2.Text = "";
TextBox3.Text = "";
}
else
{
Label4.Visible = true;
Label4.Text = "Record not Found";
}
}
else
{
adp.SelectCommand.Parameters.Add(new SqlParameter("@TextBox1", System.Data.SqlDbType.Int));
adp.SelectCommand.Parameters["@textBox1"].Value = Convert.ToInt32(TextBox1.Text);
DataSet ds = new DataSet();
adp.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
TextBox2.Text = ds.Tables[0].Rows[0]["emp_name"].ToString();
TextBox3.Text = ds.Tables[0].Rows[0]["mob_no"].ToString();
GridView1.DataSource = null;
GridView1.Visible = false;
}
else
{
Label4.Visible = true;
Label4.Text = "Record not Found";
}

}


}

}


jitendra kumar mahapatro

Jiturcm, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Umeshdwivedi on: 4/15/2011 [Member] Starter | Points: 25

Up
0
Down
i help you but you must be send a zip file your project

Latest Technology Trainer
And Part time software consultant

Jiturcm, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response