Showing posts with label button. Show all posts
Showing posts with label button. Show all posts

Sunday, March 25, 2012

Check inserted data in a SQL database

Hi im having problems as im new to ASP.NET C#

i have created a button to add details into a SQL database but i want to check the details before i insert the new values from the textboxes

can anyone help...... this is what i have to insert into the database......i just want some help to compare the user name eg... if user name exists a message will appear telling the user to change a different user name

Thanks


private void Button1_Click(object sender, System.EventArgs e)
{
string connectionString = "server=\'(local)\'; trusted_connection=true; database=\'tester\'";

//System.Data.IDbConnection conn = new System.Data.SqlClient.SqlConnection(connectionString);
System.Data.IDbConnection conn = new System.Data.SqlClient.SqlConnection(connectionString);
conn.Open();

string commandString = "INSERT INTO Users (UserName, Password) " + "Values(@.UserName, @.Password)";

//SqlCommand dbCommand = new SqlCommand (commandString, dbconn);

System.Data.IDbCommand dbCommand = new System.Data.SqlClient.SqlCommand();

//System.Data.SqlClient.SqlCommand myCmd = new System.Data.SqlClient.SqlCommand(queryString, conn);

dbCommand.CommandText = commandString;

dbCommand.Connection = conn;

SqlParameter unParam = new SqlParameter ("@.UserName", SqlDbType.NVarChar, 60);
unParam.Value = txtUser.Text;
dbCommand.Parameters.Add(unParam);
SqlParameter paParam = new SqlParameter ("@.Password", SqlDbType.NVarChar, 60);
paParam.Value = txtPassword.Text;
dbCommand.Parameters.Add(paParam);

dbCommand.ExecuteNonQuery();

conn.Close();

Response.Redirect ("WebForm1.aspx");
}

create a stored procedure, pass in userName and password to it, then make the stored procedure check if user exists and return a value.
eg:

IF NOT EXISTS (SELECT user FROM some_table WHERE <A href="http://links.10026.com/?link=mailto:user=@.userName">user=@.userName</A>)
RETURN 0 /* user doesnt exist */
ELSE
RETURN 1

then

SqlParameter ret = new SqlParameter("@.retVal", SqlDbType.Int, 4);
ret.Direction = ParameterDirection.ReturnValue;

do an ExecuteScalar on your sql command and

int status = (int)sqlCmd.Parameters["@.retVal"].Value;
if (status == 0) { // user created }
else { // failed to create user; user exists }
sql

Thursday, March 8, 2012

Check checkboxes

Hi,

I have two web pages in one web page i have 5 check boxes. For example if the user checks the Checkbox1, checkbox2 and clicks on button.

On the button click I am storing the selected checkboxes value in database lke the following:

Year Options

xxx 1

xxx 2

in the above format( user selectes checbox1, check box 2).

And in the Second Web page I am showing the 5 checkboxes but in this web page I need to check the first and second checkboxes on the page load because user selectes those two check boxes in the first web page.

my select query returning the results like this:

Options

1

2

based on options I have to check those corresponding check boxes in the second web page.

How to achive this.

Thanks in advance

Saving a user's CheckBoxList selection and re-populating the CheckBoxList from saved data:http://www.mikesdotnetting.com/Article.aspx?ArticleID=53

|||

Hello,

You need to loop thru your result set and set the checkboex to Checked on Page_Load event.

For example:

This is how you get the values and set the checkboxes to Checked.

SqlCommand cmd = new SqlCommand("Select OPtions from <Yourtable>...");

SqlDataReader reader =cmd.ExecuteReader();while( reader.Read())

{

int col = reader.GetOrdinal("Options");

int checkValue = reader.GetInt32(col);switch(checkValue)

{

case 1:

CheckBox1.Checked =true;

case 2:

CheckBox2.Checked =true;

... etc

}

}

Optionaly you can use Page.FindControl() method to get to the checkboxes (instead of using switch statement). The problem with that is that FindControl does not loop thru the Page hierarchy so if you page is complex you may have to write your own FindControl method.

Once you have that method you can call it from the Page_Load event, like this:

if (!IsPostback)
{

// Call the SetCheckBoxes method here ...

}

Hope this helps

regards,

G